package domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import org.uqbar.commons.model.ObservableObject;
import domain.extra.FullTableException;
import domain.extra.Result;

public class Table extends ObservableObject implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private Result actualNumber; 
	private int maxPlayers;
	private List<Bet> actualBets; 
	private List<Bet> bets; 
	private List<Player> players; 
	private double well; 
	private Random random;
	
	public static final String ACTUAL_NUMBER = "actualNumber";
	public static final String MAX_PLAYERS = "maxPlayers";
	public static final String PLAYERS= "players";
	public static final String ACTUAL_BETS = "actualBets";

	public Table(){
		super(); 
		this.actualBets = new LinkedList<Bet>(); 
		this.bets = new LinkedList<Bet>(); 
		this.players = new LinkedList<Player>();
		this.maxPlayers = 8;
		this.well = 1000000;
		this.random = new Random();
		
	}

	/* ***************************************** */
	/* *************** ACCIONES **************** */
	/* ***************************************** */
	
	public void addPlayer (Player player) throws FullTableException{
		if (this.players.size() < this.maxPlayers){
			this.players.add(player);
		}else {
			throw new FullTableException ("La mesa esta llena");
		}
	}
	
	public void addBet(Bet bet){
		this.actualBets.add(bet);
	}
	
	public void removePlayer (Player player){
		this.players.remove(player);
	}
	
	public Result rouletteSpin (){
		int number = random.nextInt(36);
		actualNumber= new Result(number);
		return actualNumber;
	}
	
	public void betsPay (){
		for (Bet bet : actualBets) {
			if(bet.win(this.actualNumber))
				bet.getPlayer().addGain(bet.gain());
		}
	}
	
	/* ***************************************** */
	/* *************** ATRIBUTOS *************** */
	/* ***************************************** */

	public void setActualNumber(Result actualNumber) {
		this.setFieldValue(ACTUAL_NUMBER, actualNumber);
	}
	
	public void setWell(double well) {
		this.well = well;
	}
	
	public void setMaxPlayers(int maxPlayers) {
		this.setFieldValue(MAX_PLAYERS, maxPlayers);
	}
	
	public void setActualBets(List<Bet> actualBets) {
		this.setFieldValue(ACTUAL_BETS, actualBets);
	}
	
	public void setPlayers(List<Player> players) {
		this.setFieldValue(PLAYERS, players);
	}
	
	public Result getActualNumber() {
		return actualNumber;
	}

	public int getMaxPlayers() {
		return maxPlayers;
	}

	public List<Bet> getActualBets() {
		return actualBets;
	}

	public Collection<Bet> getBets() {
		return bets;
	}

	public Collection<Player> getPlayers() {
		return players;
	}

	public double getWell() {
		return well;
	}
}
